message(NOTICE "开始构建 x265 模块")
# 检查构建目标是否存在
if(TARGET x265-${X265_TAG})
    message(NOTICE "x265 模块已存在 x265-${X265_TAG}")
    return()
endif()
# 检查生成的文件是否存在
existsInstallFiles(
    ${MODULE_STAMP}/x265/${X265_TAG}
    ${MODULE_INSTALL_DIR}/x265/${X265_TAG}
    x265-${X265_TAG})
# 配置库参数成成规则
set(X265_CONFIGURE CACHE STRING "x265 configure" FORCE)
if(BUILD_SHARED_LIBS)
    # 开启 编译动态库 ON
    list(APPEND X265_CONFIGURE "-DENABLE_SHARED=ON")
else()
    # 关闭 编译动态库 OFF
    list(APPEND X265_CONFIGURE "-DENABLE_SHARED=OFF")
endif()
if(BUILD_STATIC_LIBS)
    # 开启 编译静态库 ON
    list(APPEND X265_CONFIGURE "-DENABLE_STATIC=ON")
else()
    # 关闭 编译静态库 OFF
    list(APPEND X265_CONFIGURE "-DENABLE_STATIC=OFF")
endif()

# Android 配置 模块
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
    # 编译器工具链
    list(APPEND X265_CONFIGURE "-DANDROID_ABI=${ANDROID_ABI}")
    list(APPEND X265_CONFIGURE "-DANDROID_PLATFORM=${ANDROID_PLATFORM}")
    list(APPEND X265_CONFIGURE "-DANDROID_STL=${ANDROID_STL}")
    list(APPEND X265_CONFIGURE "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}")
    list(APPEND X265_CONFIGURE "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}")
    list(APPEND X265_CONFIGURE "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}")
    list(APPEND X265_CONFIGURE "-DCMAKE_SYSROOT=${CMAKE_SYSROOT}")
endif()

# 构建 x265 库
ExternalProject_Add(
    x265-${X265_TAG}
    # 目录配置
    PREFIX         ${EXTERNAL_PROJECT_ROOT}                # ✅ 显式设置 prefix 目录
    SOURCE_DIR     ${MODULE_SRC}/x265/${X265_TAG}          # ✅ 显式设置 source 目录
    BINARY_DIR     ${MODULE_BUILD}/x265/${X265_TAG}        # ✅ 显式设置 build 目录
    INSTALL_DIR    ${MODULE_INSTALL_DIR}/x265/${X265_TAG}  # ✅ 显式设置 install 目录
    STAMP_DIR      ${MODULE_STAMP}/x265/${X265_TAG}        # ✅ 显式设置 stamp 目录
    TMP_DIR        ${MODULE_TMP}/x265/${X265_TAG}          # ✅ 显式设置 tmp 目录
    DOWNLOAD_COMMAND # 下载命令
        COMMAND ${CMAKE_COMMAND}
            -DSOURCE_DIR=<SOURCE_DIR>
            -DGIT_REPOSITORY=${X265_REPO}
            -DGIT_SHALLOW=OFF
            -DGIT_TAG=${X265_TAG}
            -P ${__DIR}/utils/git.cmake
    CONFIGURE_COMMAND
        COMMAND ${CMAKE_COMMAND}
             -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
             -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
             -DENABLE_CLI=OFF          # 不编译命令行工具
             -DENABLE_ASM=ON           # 启用汇编优化
             # 标准 C/C++ 编译选项
             -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
             -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
             -DHIGH_BIT_DEPTH=ON       # 高位深支持
             -S <SOURCE_DIR>/source
             -B <BINARY_DIR>
            ${X265_CONFIGURE}

    BUILD_COMMAND
        ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${CMAKE_BUILD_TYPE} -j${NPROC}
    INSTALL_COMMAND
        ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${CMAKE_BUILD_TYPE}
            # 将安装文件路径保存到 文件去
            COMMAND ${CMAKE_COMMAND}
            -DINSTALL_DIR=<INSTALL_DIR>
            -DCMAKE_PREFIX_PATH=${SYSTEM_PREFIX}
            -P ${__DIR}/utils/write_install_files.cmake
    # ✅ 启用所有日志（调试必备）
    LOG_DOWNLOAD      OFF                           # 下载日志
    LOG_CONFIGURE     OFF                           # 配置日志
    LOG_BUILD         OFF                           # 构建日志
    LOG_INSTALL       OFF                           # 安装日志
    LOG_OUTPUT_ON_FAILURE ON                       # 失败时自动输出日志
   # 依赖目标 安装需要先安装 automake libtool autopoint
   STEP_TARGETS configure build install # 关键：添加 install 步骤
)


# 导入 x265 库
add_library(x265::x265 STATIC IMPORTED)
# 属性配置
set_target_properties(x265::x265 PROPERTIES
    IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
    IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/x265/${X265_TAG}/lib/libx265.a
    INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/x265/${X265_TAG}/include
)
# 依赖目标
add_dependencies(x265::x265 x265-${X265_TAG})